﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace WebApplicationTestJs.Models
{
    /// <summary>
    /// 自定義，流
    /// </summary>
    public class MyStream : Stream
    {
        public Encoder _encoder;

        /// <summary>
        /// 字符編碼
        /// </summary>
        public Encoding _encoding;

        private Stream _sm;

        public MyStream(Stream stream,Encoding encoding)
        {
            _sm = stream;
            _encoding = encoding;           
        }

        public override bool CanRead => true;

        public override bool CanSeek => true;

        public override bool CanWrite => true;

        public override long Length => _sm.Length;

        public override long Position { get => _sm.Position; set => _sm.Position = value; }

        public override void Flush()
        {
            _sm.Flush();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            //throw new NotImplementedException();
            return _sm.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            //throw new NotImplementedException();
          return  _sm.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            //throw new NotImplementedException();
            _sm.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string html = _encoding.GetString(buffer);
            //将html里面的简体中文转换为繁体,末尾參數設置為1033為了防止頁面亂碼產生                
            //string tHtml = Microsoft.VisualBasic.Strings.StrConv(html, VbStrConv.TraditionalChinese, 1033); 
            
            //压缩页面，去除换行

            byte[] bytes = _encoding.GetBytes(html);
            _sm.Write(bytes, 0, bytes.Length);
        }


    }
}